package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:Login
 * Package:com.itheima.controller
 * Description:
 *
 * @Author 老大
 * @Create 2024/5/8 9:57
 * @Version 1.0
 */
@RestController
public class Login {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp e = empService.login(emp);

        if(e == null){
            return Result.error("登陆失败,账号或密码错误请重新登录");
        }

        //创建map集合
        Map<String , Object> map = new HashMap<>();
        map.put("id" , e.getId());
        map.put("username" , e.getUsername());
        map.put("password" , e.getPassword());
        String jwt = JwtUtils.generateJwt(map);
        return Result.success(jwt);
    }
}
